import axios from 'axios'

// create an axios instance
const service = axios.create({
    baseURL: $env?.baseURL, // url = base url + request url
    // withCredentials: true, // send cookies when cross-domain requests
    timeout: 100000 // request timeout
})

// request interceptor
service.interceptors.request.use(
    config => {
        // if (store.getters.token) {
        //   config.headers['authorization'] = `Bearer ${getToken()}`
        // }
        return config
    },
    error => {
        return Promise.reject(error)
    }
)

// response interceptor
service.interceptors.response.use(
    response => {
        const res = response.data

        if (res.code && res.code !== 200) {
            if (res.code === 404) {
                alert(res.message)
                return res
            }
            alert(res.message)
            return Promise.reject(new Error(res.message || 'Error'))
        } else {
            return res
        }
    },
    error => {
        alert(error.message)
        return Promise.reject(error)
    }
)

export default service
